package com.wx.web.controller.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.wx.business.course.IxbCourseDetailService;
import com.wx.business.course.IxbCourseShareService;
import com.wx.business.course.IxbCourseVisitService;
import com.wx.business.util.XMathUtil;
import com.wx.model.AppConfig;
import com.wx.model.IxbCourseShare;
import com.wx.model.IxbUser;

/**
 * 课程管理控制器
 *
 * @author wemo
 * @Date 2018-08-08 18:27:50
 */
@Controller
@RequestMapping("/ixbshare")
public class CourseShareController {


    @Autowired
    private IxbCourseDetailService ixbCourseDetailService;
    
    @Autowired
    private IxbCourseVisitService ixbCourseVisitService;
    
    @Autowired
    private IxbCourseShareService ixbCourseShareService;

    
    /**
     * 查询机构详情
     * @param sid
     * @return
     */
   @RequestMapping(value = "/list")
   @ResponseBody
   public JsonResult shareDetailList(HttpServletRequest request) {
	   IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");
	   List<Map<String,Object>> result=ixbCourseShareService.selectMyShareCourseList(user.getSid());
	   if(result.isEmpty())
	   {
		   return new JsonResult(false,"没有分享的课程");
	   }
	   for(Map<String,Object> detail:result)
	   {
	    	detail.put("expPrice",XMathUtil.multiply(detail.get("coursePrice")+"", detail.get("discount")+"") );
			detail.put("businessPicUrl", AppConfig.BaseUrl+detail.get("businessPic"));
	   }
	   Map<String,Object>  resultMap=new HashMap<String,Object>();
	   resultMap.put("data", result);
	   return new JsonResult(true,"成功",resultMap);
	   
   }
    
   
   /**
    * 分享
    * @param sid
    * @return
    */
   @RequestMapping(value = "/add")
   @ResponseBody
   public JsonResult addShareDetail(HttpServletRequest request,String courseId) {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");

	    IxbCourseShare courseShare=new IxbCourseShare();
	    courseShare.setCourseId(courseId);
	    courseShare.setCreateTime(new Date());
	    courseShare.setSalesVolume(0);
	    courseShare.setVisitVolume(0);
	    courseShare.setBounsCharge(0.00);
	    courseShare.setShareVolume(0);
	    courseShare.setShareUserId(user.getSid());
		ixbCourseShareService.insert(courseShare);
		return new JsonResult(true,"添加成功");
   }
   

   /**
    * 总的统计
    * @param sid
    * @return
    */
   @RequestMapping(value = "/statistic")
   @ResponseBody
   public JsonResult selectSharestatistic(HttpServletRequest request) {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");

		Map<String,Object> dataMap=ixbCourseShareService.selectMyShareStatistics(user.getSid());
		
		//可提现金额，后续根据提现金额来
		dataMap.put("withDrawFee", 0);
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("resultMap", dataMap);
		return new JsonResult(true,"成功",resultMap);
   }
}
